package com.cmcc.passenger.ui.profile

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView


import com.cmcc.passenger.databinding.ListItemFailuerReasonBinding
import com.cmcc.shared.model.Msg


class ReasonAdapter : ListAdapter<Msg, ReasonViewHolder>(
    MReasonDiffCallback()
) {

    private lateinit var context: Context
    private var itemClickListener: ReasonItemClickListener? = null



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReasonViewHolder {
        this.context = parent.context
        return ReasonViewHolder(
            ListItemFailuerReasonBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )




    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onBindViewHolder(holder: ReasonViewHolder, position: Int) {
        val model = getItem(position)
        holder.binding.root.setOnClickListener( { view ->
            model.let { itemClickListener?.click(view, model) }
        })
    }

    fun removeList() {
        currentList.clear()
    }

    fun addList(records: List<Msg>) {
        currentList.addAll(records)
    }



    fun addItemClickListener(listener: ReasonItemClickListener) {
        itemClickListener = listener
    }



}
interface ReasonItemClickListener {
    fun click(view: View, model: Msg?)
}

class MReasonDiffCallback : DiffUtil.ItemCallback<Msg>() {
    override fun areItemsTheSame(msgItem: Msg, newItem: Msg): Boolean {
        return msgItem.id == msgItem.id
    }

    override fun areContentsTheSame(msgItem: Msg, newItem: Msg): Boolean {
        return msgItem.id == msgItem.id
    }
}


class ReasonViewHolder(val binding: ListItemFailuerReasonBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind() {

    }
}